package cn.tedu.tmall.front.content.dao.repository.impl;

import cn.tedu.tmall.front.content.dao.mapper.ArticleDetailMapper;
import cn.tedu.tmall.front.content.dao.mapper.ArticleMapper;
import cn.tedu.tmall.front.content.dao.repository.IArticleRepository;
import cn.tedu.tmall.front.content.pojo.po.ArticleDetailPO;
import cn.tedu.tmall.front.content.pojo.po.ArticlePO;
import cn.tedu.tmall.front.content.pojo.vo.PageDataVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class ArticleRepository implements IArticleRepository {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleDetailMapper articleDetailMapper;

    @Override
    public PageDataVO<ArticlePO> listArticleByCategoryId(Long categoryId, Integer pageNum, Integer pageSize) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("category_id",categoryId);
        PageHelper.startPage(pageNum,pageSize);
        List list =  articleMapper.selectList(queryWrapper);
        //log.debug("list:{}",list);
        PageInfo<ArticlePO> pageInfo = new PageInfo<>(list);
       log.debug("pageInfo:{}",pageInfo);
        PageDataVO pageDataVO1 = new PageDataVO();
        BeanUtils.copyProperties(pageInfo,pageDataVO1);
        log.debug("封装后pageDataVO:{}",pageDataVO1);
        //log.debug("没有封装pageDataVO:{}",pageDataVO2);
        return pageDataVO1;
    }

    @Override
    public ArticlePO getArticleById(Long id) {
        return  articleMapper.selectById(id);
    }

    @Override
    public ArticleDetailPO getArticleDetail(Long articleId) {
        return  articleDetailMapper.selectById(articleId);
    }
}
